package ru.inse.core.dao.realty.impl;

import java.util.List;

import ru.inse.core.dao.ICriteriaAppender;
import ru.inse.core.dao.QueryBuilder;
import ru.inse.core.dao.realty.IRealtyItemCriteraAppenderFactory;
import ru.inse.core.dao.realty.RealtyQueryParam;
import ru.inse.core.domain.realty.RealtyObject;
import ru.inse.core.domain.realty.RealtyRubric;


public class RealtyCriteraAppenderFactory
    implements IRealtyItemCriteraAppenderFactory
{
    
    
    @Override
    public ICriteriaAppender createFindByUserCriteriaAppender(final Long userId)
    {
        return new ICriteriaAppender()
        {
            @Override
            public String append(String sql,  List<Object> params)
            {
                params.add(userId);
                return sql + " where user_id=?";
            }
        };
    }

    @Override
    public ICriteriaAppender createCriteriaAppender(
        final RealtyRubric rubric, 
        final RealtyObject object, 
        final RealtyQueryParam param)
    {
        return new ICriteriaAppender()
        {
            @Override
            public String append(String sql,  List<Object> params)
            {
                StringBuilder sb = new StringBuilder(sql);
                QueryBuilder queryBuilder = new QueryBuilder(sb, params);
                
                if (rubric != null)
                {
                    queryBuilder.and().eq("rubric_id", rubric.getCode());
                }

                if (object != null)
                {
                    queryBuilder.and().eq("object_id", object.getId());
                }

//                Criteria placeCriteria = c.createCriteria("place", "p");
//                placeCriteria.add(Restrictions.eq("city", cityDao.load(cityId)));
//                SelectOnStep join = step.join("place");
//                join.on(Factory.field("id").equal("city_id").and(Factory.field("city_id").equal(param.getCityId())));
                
                if (param != null)
                {
                    String street = param.getPlaceName();
                    if (street != null && street.length() > 0)
                    {
                    }

                    // City
                    if (param.getCityId() != null)
                    {
                        queryBuilder.and().eq("city_id", param.getCityId());
                    }
                    // Price
                    if (param.getLoPrice() != null)
                    {
                        queryBuilder.and().ge("price", param.getLoPrice());
                    }
                    if (param.getHiPrice() != null)
                    {
                        queryBuilder.and().le("price", param.getHiPrice());
                    }
                    // Area
                    if (param.getLoArea() != null)
                    {
                        queryBuilder.and().ge("area", param.getLoArea());
                    }
                    if (param.getHiArea() != null)
                    {
                        queryBuilder.and().le("area", param.getHiArea());
                    }
                    // i1
                    if (param.getLoI1() != null)
                    {
                        queryBuilder.and().ge("i1", param.getLoI1());
                    }
                    if (param.getHiI1() != null)
                    {
                        queryBuilder.and().le("i1", param.getHiI1());
                    }
                    // i2
                    if (param.getLoI2() != null)
                    {
                        queryBuilder.and().ge("i2", param.getLoI2());
                    }
                    if (param.getHiI2() != null)
                    {
                        queryBuilder.and().le("i2", param.getHiI2());
                    }

                }
                return sb.toString();
            }
        };
    }

}
